package com.spongebob.controller.xiao;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.spongebob.pojo.CustomerService;
import com.spongebob.pojo.Session;
import com.spongebob.pojo.SessionMessage;
import com.spongebob.service.SessionMessageService;
import com.spongebob.service.SessionService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 会话管理 前端控制器
 * </p>
 *
 * @author tutu
 * @since 2021-03-16
 */
@Controller
@RequestMapping("/session-message")
public class SessionMessageController {

    @Resource
    SessionMessageService sessionMessageService;
    @Resource
    private SessionService sessionService;

    /**
     * 根据id获取对话信息
     *
     * @param session
     * @param id
     * @return
     */
    @RequestMapping("/get")
    public String getSelfsession(HttpSession session, Integer id) {
        CustomerService user = (CustomerService) session.getAttribute("user");
        //查对话
        QueryWrapper<SessionMessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sessionid", id);
        List<SessionMessage> list = sessionMessageService.list(queryWrapper);

        session.setAttribute("content", list);
        //设置顾客信息
        QueryWrapper<Session> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("sessionid", id);
        Session one = sessionService.getOne(queryWrapper1);
        session.setAttribute("visitorip", one.getVisitorip());
        session.setAttribute("sessionid", id);
        session.setAttribute("name", one.getVisitorname());
        session.setAttribute("active", user.getState());
        return "redirect:/session/get";
    }

    /**
     * 发送消息
     *
     * @param content
     * @param session
     * @return
     */
    @RequestMapping("/submit")
    public String submit(String content, HttpSession session) {
        CustomerService user = (CustomerService) session.getAttribute("user");
        if (content == null) {
            return "redirect:/session/get";
        }
        SessionMessage sessionMessage = new SessionMessage();
//保持消息
        sessionMessage.setVisitorip((String) session.getAttribute("visitorip"));
        sessionMessage.setContent(content);
        sessionMessage.setSessionid((Integer) session.getAttribute("sessionid"));
        sessionMessage.setFromvisitor(!user.getServicerole().equals("系统管理员"));
        sessionMessage.setCustomerserviceid(user.getCustomerserviceid());
        sessionMessage.setSendtime(new Date());
        sessionMessageService.save(sessionMessage);
        List<SessionMessage> list = (List<SessionMessage>) session.getAttribute("content");
        list.add(sessionMessage);
        //更新最新发送时间
        UpdateWrapper<Session> w = new UpdateWrapper<>();
        w.eq("sessionid", (Integer) session.getAttribute("sessionid"));
        w.set("createtime", new Date());
        sessionService.update(w);
        return "redirect:/session/get";
    }


}

